package com.xique.door.controller;

import com.alibaba.fastjson.JSONObject;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.api.request.device.RemoteDeviceListRequest;
import com.xique.door.bean.request.device.*;
import com.xique.door.biz.service.IDeviceBizService;
import com.xique.door.device.freeview.service.biz.FreeviewBizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author shd
 * @create 2022/1/25 10:16
 */

@RestController
@RequestMapping("/device")
public class DeviceController extends BaseController {

    @Autowired
    private IDeviceBizService deviceBizService;

    @Autowired
    private FreeviewBizService freeviewBizService;


    @PreAuthorize(hasPermi = "door:device:list")
    @GetMapping("/list")
    public AjaxResult list(DeviceListRequest request) {
        startPage();
        return deviceBizService.getDeviceList(request);
    }

    @PreAuthorize(hasPermi = "door:device:edit")
    @GetMapping("/detail")
    public AjaxResult detail(Long deviceId) {
        return deviceBizService.getDeviceDetail(deviceId);
    }

    @PreAuthorize(hasPermi = "door:device:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@Validated @RequestBody DeviceAddRequest request) {
        return deviceBizService.addDevice(request);
    }

    @PreAuthorize(hasPermi = "door:device:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/edit")
    public AjaxResult edit(@Validated @RequestBody DeviceEditRequest request) {
        return deviceBizService.editDevice(request);
    }

    @PreAuthorize(hasPermi = "door:device:remove")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/del")
    public AjaxResult delete(@Validated @RequestBody DeviceDeleteRequest request) {
        return deviceBizService.deleteDevice(request);
    }

    @PreAuthorize(hasPermi = "door:device:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/changeStatus")
    public AjaxResult changeStatus(@Validated @RequestBody DeviceChangeStatusRequest request) {
        return deviceBizService.changeStatus(request);
    }

    @PreAuthorize(hasPermi = "door:device:edit")
    @Log(title = "设备重启", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/reboot")
    public AjaxResult reboot(@RequestBody String json) {
        return deviceBizService.reboot(json);
    }

    @Log(title = "全视通设备固件升级", businessType = BusinessType.UPDATE)
    @PostMapping("/upgrade")
    @ResponseBody
    public AjaxResult freeviewUpgrade(@RequestParam(value = "devSn") String devSn,
                                      @RequestParam(value = "file") MultipartFile file) {

        return freeviewBizService.freeviewUpgrade(devSn, file);
    }

    @PreAuthorize(hasPermi = "door:device:list")
    @GetMapping("/getAll")
    public AjaxResult getAll() {
        return deviceBizService.getAllDevices(SecurityUtils.getParkId());
    }

    @PreAuthorize(hasPermi = "door:device:list")
    @GetMapping("/deviceList")
    @ResponseBody
    public AjaxResult deviceList(Long id) {
        return deviceBizService.getDeviceList(id);
    }

    @PostMapping("/openApi/v1/list")
    public AjaxResult deviceListV1(@RequestBody RemoteDeviceListRequest request) {
        return deviceBizService.getDeviceListV1(request);
    }


    @PostMapping(value = "/openDoor")
    public AjaxResult openDoor(@RequestBody String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        String devSn = String.valueOf(jsonObject.get("devSn"));
        return deviceBizService.openDoor(devSn, SecurityUtils.getUserId());
    }
}
